/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.dao.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import ar.com.greensoft.neo.dao.PersonaJuridicaDao;
import ar.com.greensoft.neo.model.PersonaJuridica;
import ar.com.greensoft.neo.model.PersonaJuridica_;
import ar.com.greensoft.neo.search.SearchInput;
import ar.com.greensoft.neo.search.user.PersonaJuridicaSearchInput;
import ar.com.greensoft.neo.util.SearchUtil;

/**
 * @author Jorge E. Villaverde
 *
 */
@Transactional
@Repository
public class JpaPersonaJuridicaDaoImpl extends AbstractJpaWithRevisionDaoImpl<PersonaJuridica> implements PersonaJuridicaDao {

	@Override
	protected Class<PersonaJuridica> getEntityClazz() {
		return PersonaJuridica.class;
	}

	@Override
	protected String getCountNamedQuery() {
		return PersonaJuridica.COUNT_QUERY_NAME;
	}

	@Override
	protected String getListNamedQuery() {
		return PersonaJuridica.FIND_ALL_QUERY_NAME;
	}

	@Override
	public List<PersonaJuridica> findBySearchInput(SearchInput input) {
		return null;
	}

	@Override
	protected Expression<Boolean> createWhereFromSearchInput(SearchInput input, Root<PersonaJuridica> root) {
		PersonaJuridicaSearchInput pfsi = (PersonaJuridicaSearchInput)input;
		
		CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
		Predicate predicate = null;
		Predicate predNombre = null;

		if(pfsi.isBuscarNombre() && StringUtils.hasText(pfsi.getNombre())){
			String searchText = SearchUtil.likeString(pfsi.getNombre());
			predNombre = criteriaBuilder.like(root.get(PersonaJuridica_.nombre), searchText);
			
			if(predicate == null){
				predicate = predNombre;
			}
		}
				
		return predicate;
	}

}
